package ua.cs.stu.ics.label;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 20.02.11
 * Time: 20:29
 * To change this template use File | Settings | File Templates.
 */
public class JLabelExtended extends JLabel {

    JPopupMenu popupNormal = null;
    JPopupMenu popupMove = null;

    private JMenuItem itemNormalEdit;
    private JMenuItem itemNormalMove;
    private JMenuItem itemMoveSave;
    private JMenuItem itemMoveCancel;

    private boolean fromDatabase = true;

    private MouseAdapter normalMouseAdapter;
    private MyMouseAdapter moveMouseAdapter;

    static private final int STATUS_NORMAL = 0;
    static private final int STATUS_MOVE = 2;


    private int status = 0;
    private JLabelExtended jLabelExtended;



    int moveInitialCoordinateX;
    int moveInitialCoordinateY;
    int lastX, lastY;

    public JLabelExtended(){
        this("");
    }

    public JLabelExtended(boolean fromDatabase) {
        this("");
        this.fromDatabase = fromDatabase;

    }

    public JLabelExtended(boolean fromDatabase, String text) {
        this(text);
        this.fromDatabase = fromDatabase;
    }

    public JLabelExtended(String text) {
        super(text);
        jLabelExtended = this;

        popupNormal = new JPopupMenu();

        itemNormalEdit = new JMenuItem("Редактировать");
        itemNormalMove = new JMenuItem("Переместить");

        popupNormal.add(itemNormalEdit);
        popupNormal.add(itemNormalMove);

        popupMove = new JPopupMenu();

        itemMoveSave = new JMenuItem("Сохранить");
        itemMoveCancel = new JMenuItem("Отменить");

        popupMove.add(itemMoveCancel);
        popupMove.add(itemMoveSave);


        itemNormalMove.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jLabelExtended.setStatus(STATUS_MOVE);
                jLabelExtended.removeMouseListener(normalMouseAdapter);
                jLabelExtended.addMouseListener(moveMouseAdapter);
                jLabelExtended.addMouseMotionListener(moveMouseAdapter);
                jLabelExtended.setForeground(Color.BLUE);
                moveInitialCoordinateX = jLabelExtended.getX();
                moveInitialCoordinateY = jLabelExtended.getY();


            }
        });

        itemMoveCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jLabelExtended.setForeground(Color.BLACK);
                jLabelExtended.setStatus(STATUS_NORMAL);
                jLabelExtended.removeMouseListener(moveMouseAdapter);
                jLabelExtended.removeMouseMotionListener(moveMouseAdapter);
                jLabelExtended.addMouseListener(normalMouseAdapter);

                jLabelExtended.setLocation(moveInitialCoordinateX, moveInitialCoordinateY);
            }
        });

        itemMoveSave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jLabelExtended.setForeground(Color.BLACK);
                jLabelExtended.setStatus(STATUS_NORMAL);
                jLabelExtended.removeMouseListener(moveMouseAdapter);
                jLabelExtended.removeMouseMotionListener(moveMouseAdapter);
                jLabelExtended.addMouseListener(normalMouseAdapter);

            }
        });

        itemNormalEdit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String s = (String)JOptionPane.showInputDialog(null,null,"Редактирование компонента",1, null, null, jLabelExtended.getText());
                if (s != null){
                    jLabelExtended.setText(s);
                }
            }
        });


        normalMouseAdapter = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == 3) {
                    popupNormal.show(e.getComponent(), e.getX(), e.getY());
                    lastX = jLabelExtended.getX();
                    lastY = jLabelExtended.getY();
                }
            }
        };


        moveMouseAdapter = new MyMouseAdapter();

        this.addMouseListener(normalMouseAdapter);
    }

    private void setStatus(int status) {
        this.status = status;
    }

    public void setFromDatabase(boolean fromDatabase) {
        this.fromDatabase = fromDatabase;
    }

    public boolean isFromDatabase() {
        return fromDatabase;
    }
}
